package com.yyge.config.security.login;

import com.yyge.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderNotFoundException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * <p> 自定义认证管理器 </p>
 *
 * @author : gityyge
 * @description :
 * @date : 2019/10/12 14:49
 */
@Component
@Slf4j
public class CusAuthenticationManager implements AuthenticationManager {

    @Autowired
    private IUserService userService;

    private final AuthProvider authProvider;

    public CusAuthenticationManager(AuthProvider authProvider) {
        this.authProvider = authProvider;
    }

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        Authentication result = authProvider.authenticate(authentication);
        //userMapper.updateById()
        if (Objects.nonNull(result)) {
            return result;
        }
        log.error("Authentication failed!");
        throw new ProviderNotFoundException("Authentication failed!");
    }

}
